﻿using Newtonsoft.Json;
using PDFDesign.Model;
using PDFDesign.Properties;
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace PDFDesign.Common
{
    public class DesignUtils
    {
        /// <summary>
        /// 清除TreeView选择
        /// </summary>
        /// <param name="tv"></param>
        public static void ClearTreeViewSelection(TreeView tv)
        {
            if (tv != null)
                ClearTreeViewItemsControlSelection(tv.Items, tv.ItemContainerGenerator);
        }

        private static void ClearTreeViewItemsControlSelection(ItemCollection ic, ItemContainerGenerator icg)
        {
            if ((ic != null) && (icg != null))
                for (int i = 0; i < ic.Count; i++)
                {
                    if (icg.ContainerFromIndex(i) is TreeViewItem tvi)
                    {
                        ClearTreeViewItemsControlSelection(tvi.Items, tvi.ItemContainerGenerator);
                        tvi.IsSelected = false;
                    }
                }
        }

        /// <summary>
        /// 查询指定类型子控件
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="depObj"></param>
        /// <returns></returns>
        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
        {
            if (depObj != null)
            {
                for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
                    if (child != null && child is T)
                    {
                        yield return (T)child;
                    }
                    foreach (T childOfChild in FindVisualChildren<T>(child))
                    {
                        yield return childOfChild;
                    }
                }
            }
        }

        /// <summary>
        /// 获取保存数据列表
        /// </summary>
        /// <returns></returns>
        public static List<SaveData> GetSaveDataList()
        {
            var list = new List<SaveData>();
            if (string.IsNullOrWhiteSpace(Settings.Default.DataList)) return list;
            try
            {
                list = JsonConvert.DeserializeObject<List<SaveData>>(Settings.Default.DataList);
            }
            catch (Exception) { }
            return list;
        }

        /// <summary>
        /// 添加保存数据
        /// </summary>
        /// <param name="name"></param>
        /// <param name="data"></param>
        public static void AddSaveData(string name, string data)
        {
            var saveData = new SaveData { Name = name, Data = data };
            var dataList = GetSaveDataList();
            dataList.Add(saveData);
            Settings.Default.DataList = JsonConvert.SerializeObject(dataList);
            Settings.Default.Save();
        }
    }
}
